package com.atguigu1;

import org.junit.Test;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @auther user
 * @date 2021/12/5 - 19:23
 */
public class ConnectionTest {
    @Test
    public void testConnection() throws Exception{
        //1 获取Driver实现类对象
        Driver driver = new com.mysql.jdbc.Driver();
        //2 提供连接四要素
        //Drover
        String url = "jdbc:mysql://8.130.54.82:3306/test?characterEncoding=utf-8&rewriteBatchedStatements=true";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","hwychu1998917Aa");

        //3 获取连接
        java.sql.Connection connection = driver.connect(url, properties);
        System.out.println("connection = " + connection);
    }

    //方式二 反射 在如下的程序中不会出现第三方api,使程序有更好的移植性
    @Test
    public void testConnection2() throws  Exception{
        //1 获取Driver的实现类对象
        Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //2 提供连接数据库的四要素
        String url = "jdbc:mysql://8.130.54.82:3306/test?characterEncoding=utf-8&rewriteBatchedStatements=true";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","hwychu1998917Aa");
        //3 获取连接
        java.sql.Connection connection = driver.connect(url, properties);
        System.out.println("connection = " + connection);
    }
    //方式三 使用DriverManager替换Driver
    @Test
    public void testConnection3() throws  Exception {
        System.out.println("connection.testConnection3");
        //1 获取Driver实现类对象
        Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //2 提供连接的三个基本要素
        String url = "jdbc:mysql://8.130.54.82:3306/test?characterEncoding=utf-8&rewriteBatchedStatements=true";
        String user = "root";
        String password = "hwychu1998917Aa";
        //3 注册驱动
        DriverManager.registerDriver(driver);
        //4 获取连接
        java.sql.Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("connection = " + connection);
    }
    //方式四 可以只是加载驱动，不用显示注册驱动
    @Test
    public void testConnection4() throws  Exception{
        //1 连接的三个基本要素
        String url = "jdbc:mysql://8.130.54.82:3306/test?characterEncoding=utf-8&rewriteBatchedStatements=true";
        String user = "root";
        String password = "hwychu1998917Aa";
        //2 加载Driver
        Class.forName("com.mysql.jdbc.Driver");
/*        Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();
        //注册驱动
        DriverManager.registerDriver(driver);
        为什么不需要注册驱动了
         在MySQL的Driver实现类中，声明了如下操作
		static {
			try {
				java.sql.DriverManager.registerDriver(new Driver());
			} catch (SQLException E) {
				throw new RuntimeException("Can't register driver!");
			}
		}
*/
        //3 获取连接
        java.sql.Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("connection = " + connection);
    }
    //方式五 final 将数库的四个基本要素声明在配置文件中，通过读取配置文件的方式获取连接
    /**
     * 好处
     *  1 实现数据与代码的分离 解耦
     *  2 如果需要修改配置，避免重新打包
     */
    @Test
    public void testConnection5() throws  Exception{
        //1 读取配置文件
        //类加载器 这里是系统类加载器
        InputStream inputStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");
        //2 加载驱动
        Class.forName(driverClass);
        //3 获取连接
        Connection connection = DriverManager.getConnection(url, user, password);
        System.out.println("connection = " + connection);
    }
}
